(function () {
    var module = {};
    var tools = Tools();
    var balance;
    var mobileCode = 0;
    var telPhone;
    var czToken;
    var smsBtn = false;
    var InterValObj;
    $('.isHasPhone').hide();
    $('.up_phone').hide();
    var isShowPhone=false;
    module.init = function () {
        tools.getUserInfo();
        tools.doGet(accountURL+self.apiURL.queryUserInfo,{},module.queryUserInfo);

        //提现弹框
        $(document).on('click','.pop_regTX,.unpop_regTX',function () {
            $('.pop_error_cz').hide();
            $('.pop_error_tx').hide();
            tools.doGet(accountURL +tools.apiURL.queryAccountInfo, {}, module.queryAccountInfo);
            $('#withMoney').val('');

        });


        //充值弹框
        $(document).on('click','.pop_regCZ,.unpop_regCZ',function () {
            $('.pop_error_cz').hide();
            $('.pop_error_tx').hide();
            $('#wy_money').val('');
        });



        // 界面展示   tab   及  按钮  点击跳转
        $(document).on("click", ".toRecharge", function () {
            $('#rechargeTable').show();
            $('.bank_from_form').show();
            $('#withdrawalsTable').hide();
            $('#wy_money').val('');
            $('#kj_money').val('');
            $('#phones').val('');
            $('#User_mobileCode').val('');
            $('#rechargeSuccess').hide();
            $('#tab').show();
        });
        $(document).on("click", ".toWithdrawals", function () {
            //查询提现额度
            tools.doGet(accountURL +tools.apiURL.queryAccountInfo, {}, module.queryAccountInfo);
            $('#rechargeTable').hide();
            $('.bank_from_form').hide();
            $('#withdrawalsTable').show();
            $('#withMoney').val('');
            $('#withdrawalsSuccess').hide();
            $('#myForm').show();
        });

        $(document).on("click", ".bank_from_close", function () {
            $('#rechargeTable').show();
            $('.bank_from_form').show();
            $('#bank_list').hide();
        });
        $(document).on("click", "#recharge_kj", function () {
            $('.recharge_kj').show();
            $('.recharge_wy').hide();
            $('#recharge_kj').addClass('cur');
            $('#recharge_kj').removeClass('tabChange1');
            $('#recharge_wy').removeClass('cur');
            $('.recharge_other').addClass('tabChange');
            $('.recharge_other').removeClass('tabChange2');
        });
        $(document).on("click", "#recharge_wy", function () {
            $('.recharge_kj').hide();
            $('.recharge_wy').show();
            $('#recharge_kj').removeClass('cur');
            $('.recharge_other').addClass('tabChange2');
            $('#recharge_kj').addClass('tabChange1');
            $('#recharge_wy').addClass('cur');
            $('.recharge_other').addClass('tabChange')
        });


        //更改预留手机号
        $(document).on("click", ".up_phone", function () {
            $('.up_phone').hide();
            $('.isHasPhone').show();
            isShowPhone=true;

        });

        check();
       };

    module.queryUserInfo=function (data) {
        if (data.data.custodyStatus == 11 || data.data.custodyStatus == 0 || data.data.custodyStatus == '-1') {
            $('body').after('<div class="pop_error_oa goOpenAccount"><p class="pop_error_oa error_oa"></p> <div class="pop_oa"> <div > <a href="javascript:;" target="_blank" class="pop_op openAccount_open">存管开户</a> <p class="pop_fg"></p> <a href="javascript:;" class="pop_wait openAccount_close">以后再说</a> </div> </div> </div>');
        } else 	if(data.data.custodyStatus==111||data.data.custodyStatus==2){
            $('body').after('<div class="pop_error_oa goOpenAccount"><p class="pop_error_oa error_oa"></p> <div class="pop_oa"> <a href="openAccount.html" target="_blank" class="pop_op1 openToShanghai">存管开户</a> </div> </div>');
        }else if (data.data.custodyStatus == 1) {
            //查询提现额度
            tools.doGet(accountURL +tools.apiURL.queryAccountInfo, {}, module.queryAccountInfo);

            //查询银行卡
            tools.doGet(accountURL +tools.apiURL.bankCard, {}, module.bankCard);

            //获取提现规则
            tools.doGet(ajaxjsURL +tools.apiURL.getConfigureData, {type:3}, module.getConfigureData);

        }
        //开户弹窗   点击俩按钮
        $(document).on("click", ".openAccount_open", function () {
            $('.openAccount_open').attr('href', 'openAccount.html')
        });
        $(document).on("click", ".openAccount_close", function () {
            $('.goOpenAccount').hide();
            window.history.back();
        });
    };
    module.getConfigureData=function (data) {
        var content=data.data;
        if (content == '' || content == null || content == 'null') {
            $('.tx_gz').append('<li><p>1、为防止恶意提现，每天可提现3次。</p></li><li><p>2、单笔提现上限100万元。</p></li><li><p>3、正常工作日提现将在T+1个工作日到达银行卡；如遇双休日或者节假日顺延至下个工作日到达银行卡，具体到达银行卡时间以银行处理时间为准。</p></li>')
        } else {
            $('.tx_gz').append('<li><p>' + content.replace(/\n/g, '<br />') + '</p></li>')
        }
    };

    module.bankCard = function (data) {
        if (data.status == 0) {
            if(data.data!=null){
                var content = data.data;
                telPhone=content.tel;
                if(content.singleUpperLimit!=null&&content.dayUpperLimit!=null){
                    $('.backImg').append('<div style="position: absolute;left: 0;top: 42px;font-size: 12px;color:#aaa;">单笔限额：<label style="margin-right:15px">'+content.singleUpperLimit+'</label>每日限额：<label>'+content.dayUpperLimit+'</label></div>')
                }
                if(telPhone==''||telPhone==null||telPhone==undefined){
                    isShowPhone=true;
                    $('.isHasPhone').show();
                    $('.up_phone').hide();
                }else{
                    $('#phones').val(telPhone);
                    $('.isHasPhone').hide();
                    $('.up_phone').show();
                }
                var  str;
                if(content.name.length>6){
                    str = content.name.substr(0,6) + '...' ;
                }else{
                    str = content.name;
                }
                $('.backNum').append('<span class="bankName">'+str+'</span><span class="bankNum1">('+content.cardNo.substr(content.cardNo.length-4)+')</span>');
                $('.afterDown').after(' <img src="'+content.icon+'" class="backIcon">');

                //直接跳转提现
                if (window.location.href.indexOf('withdrawals') > 0) {
                    $('#rechargeTable').hide();
                    $('#withdrawalsTable').show();
                }
            }
        }
    };

    function ismoney(value) {
        var mny = /^\d+(\.\d+)?$/;
        return mny.test(value)
    }

    function check() {
        //银行卡
        $(document).on('keyup', '#User_card', function () {
            toCheckBank();
        });

        //快捷充值
        $(document).on('click','.subCz',function () {
            var money = $('#kj_money').val();
            var tag=true;
            if(money==''||money==null||money==undefined){
                tag=false;
                $("#message").text('请输入您的提现金额');
            }else if(!ismoney($('#kj_money').val())){
                $("#message").text('金额输入有误！');
                tag=false;
            }
            if (!tag) {//smsBtn == 'false' || smsBtn == false||
                $("#alertMessage").fadeIn();
                $("#message").text('请先获取短信验证码');
                setTimeout(function () {
                    $("#alertMessage").fadeOut();
                }, 2000);
                return false;
            } else {
                status=true;
                formBank();
            }
        });

        //网银充值
        $(document).on('click','.subWy',function () {
            var money = $('#wy_money').val();
            var tags=true;
            if(money==''||money==null||money==undefined){
                tags=false;
                $("#message").text('请输入您的提现金额');
            }else if(!ismoney($('#wy_money').val())){
                $("#message").text('金额输入有误！');
                tags=false;
            }
            if(tags){

                $(".recharge_error").text('');
                $('.pop_error_cz').show();
                //网银充值
                toNewPageCt();

            }else{
                $("#alertMessage").fadeIn();
                setTimeout(function(){
                    $("#alertMessage").fadeOut();
                },2000);
            }
        });


        //提现
        $(document).on('click','.subTx',function () {
            var tag=true;
            if($('#withMoney').val()>balance){
                $("#message").text('提现金额不能大于用户余额');
                tag=false;
            }else if($('#withMoney').val()==''||$('#withMoney').val()==null||$('#withMoney').val()==undefined){
                tag=false;
                $("#message").text('请输入您的提现金额');
            }else if(!ismoney($('#withMoney').val())) {
                $("#message").text('金额输入有误！');
                tag = false;
            }else if($('#withMoney').val()>1000000){
                $("#message").text('单笔提现上限100万元！');
                tag = false;
            }
            if(tag){
                $('.pop_error_tx').show();
                toNewPageTx();
            }else{
                $("#alertMessage").fadeIn();
                setTimeout(function(){
                    $("#alertMessage").fadeOut();
                },2000);
            }
        });

        //验证码
        $(document).on('click','#mobileCodeBnt',function () {
            mobileCode++;
            if(mobileCode!=1){
                return;
            }

            var money = $('#kj_money').val();
            if (money == '') {
                mobileCode=0;
                $("#alertMessage").fadeIn();
                $("#message").text('充值金额不能为空');
                setTimeout(function () {
                    $("#alertMessage").fadeOut();
                }, 2000);
                return false;
            }
            if(isShowPhone){
                var mobiles = /^(1)[0-9]{10}$/;
                var obusers=$('#phones').val();
                telPhone=$('#phones').val();
                var tag=true;
                if(obusers == '' || obusers == undefined){
                    mobileCode=0;
                    $("#message").text('请输入手机号');
                    tag=false;
                }else if(!mobiles.test(parseInt(obusers))){
                    tag=false;
                    mobileCode=0;
                    $("#message").text('手机号输入有误');
                }
                if(!tag){
                    $("#alertMessage").fadeIn();
                    setTimeout(function(){
                        $("#alertMessage").fadeOut();
                    },2000);
                    return;
                }
            }

            $('#kj_money').attr('readonly', true);

            $('#User_mobileCode').val('');
            $('.formgain').removeAttr("id");
            $('.formgain').attr('disabled', true);
            doPost(accountURL +tools.apiURL.rechargeStep1, {'money': money, 'mobile': telPhone}, module.step1Recharge);
        });

        $(document).on('click','#goReCz',function () {
            $('#tab').show();
            $('#rechargeSuccess').hide();
            $('#kj_money').attr('readonly', false);
            tools.doGet(accountURL +tools.apiURL.bankCard, {}, module.bankCard1);
        })

    }

    function toNewPageTx() {
        var UrLs='any.html';
        var money=$('#withMoney').val();
        var newWindow = window.open(UrLs+'?newPage=Tx&num='+money);

    }
    function toNewPageCt() {
        var UrLs='any.html';
        var money=$('#wy_money').val();
        var newWindow = window.open(UrLs+'?newPage=Cz&num='+money);

    }
    module.bankCard1 = function (data) {
        if (data.status == 0) {
            if(data.data!=null){
                var content = data.data;
                telPhone=content.tel;
                if(telPhone==''||telPhone==null||telPhone==undefined){
                    isShowPhone=true;
                    $('.isHasPhone').show();
                    $('.up_phone').hide();
                }else{
                    isShowPhone=false;
                    $('.isHasPhone').hide();
                    $('.up_phone').show();
                }
            }
        }
    };
    //校验银行卡
    function toCheckBank() {
        var tag = true;
        if ($.trim($('#User_card').val()).length == 0) {
            tag = false;
            mobileCode = 0;
        } else {
            if (tools.luhmCheck($.trim($('#User_card').val())) == false) {
                tag = false;
                mobileCode = 0;
            }
        }
        if (tag) {
            $('.correctCard').removeClass('onError');
            $('.correctCard').addClass('onRight');
        } else {
            $('.correctCard').removeClass('onRight');
            $('.correctCard').addClass('onError');
        }
        return tag;
    }


    //查询平台支持的所有银行
    module.supportBankList = function (data) {
        if (data.status == 0) {
            var content = data.data;
            if (content.length > 0) {
                var pro = '';
                for (var i = 0; i < content.length; i++) {
                    pro += '<tr><td>' + content[i].bankName + '</td> <td>借记卡</td> <td>' + content[i].singleUpperLimit + '</td> <td>' + content[i].dayUpperLimit + '</td> </tr>';
                }
                $('#bank_explain').append(pro);
            }
        }
    };

    /*--------------------------------充值------------------------------------------*/

    //充值第一步
    var status = true;
    module.step1Recharge = function (data) {
        if (data.status == 0) {
            mobileCode=0;
            smsBtn = true;
            czToken=data.data;
            $("#orderNo").val(data.data);
            smsStep(data);
            $('#kj_money').attr('readonly',true);
        } else {
            mobileCode=0;
            status = true;
            $("#alertMessage").fadeIn();
            $('#kj_money').attr('readonly', false);
            $("#message").text(data.message);

            $(".formgain").empty().val("重新发送");
            $('.formgain').attr('id', 'mobileCodeBnt').attr('disabled', false);

            setTimeout(function () {
                $("#alertMessage").fadeOut();
            }, 2000)
        }
    };

    function formBank() {
        var form ={};
        if (status == true) {
            status = false;
            form.payOrderNo=czToken;
            form.phoneCode=$('#User_mobileCode').val();
            doPost(accountURL + tools.apiURL.rechargeStep2, form, module.step2ReCharge);
            $('#accountLoading').show();
            setTimeout(function () {
                $('#accountLoading').hide();
            }, 5000)

        }
    }
    //充值第二步
    module.step2ReCharge = function (data) {
        if (data.status == 0 || data.status == "0") {
            $('#accountLoading').hide();
            status = false;
            $('#tab').hide();
            $('#rechargeSuccess').show();

            $('#orderNo').val('');
            window.clearInterval(InterValObj);//停止计时器

            $(".formgain").empty().val("重新发送");
            $('.formgain').attr('id', 'mobileCodeBnt').attr('disabled', false);

            if ($('#orderNo').val() == '') {
                $('#kj_money').attr('readonly', true);
            }
            code = "";
            $('#kj_money').val('');
            $('#phones').val('');
            $('#User_mobileCode').val('');
        } else {
            $('#accountLoading').hide();
            status = true;
            $("#alertMessage").fadeIn();
            if(data.status=='I_AC_777'){
                $("#message").text('验证码输入有误！');
            }else {
                $("#message").text(data.message);

                $('#orderNo').val('');

                window.clearInterval(InterValObj);//停止计时器

                $(".formgain").empty().val("重新发送");
                $('.formgain').attr('id', 'mobileCodeBnt').attr('disabled', false);

                if ($('#orderNo').val() == '') {
                    $('#kj_money').attr('readonly', true);
                }
                code = "";
            }
        }
        setTimeout(function () {
            $("#alertMessage").fadeOut();
        }, 2000)
    };






    //短信
    function smsStep(data) {

        var count = 60;
        var curCount;
        var code = "";
        var curCount = count;

        if (data.status == '0') {

            $(".formgain").empty().val("" + curCount + "秒重新发送");
            InterValObj = window.setInterval(SetRemainTime, 1000);

        } else {
            $('.formgain').attr('id', 'mobileCodeBnt');
            $('.formgain').attr('disabled', false);
            $('#money').attr('readonly', false);
            $("#alertMessage").fadeIn();

            $("#message").text(data.message);
            setTimeout(function () {
                $("#alertMessage").fadeOut();
            }, 2000)

        }

        //timer处理函数
        function SetRemainTime() {
            if (curCount == 0) {
                window.clearInterval(InterValObj);//停止计时器

                $(".formgain").empty().val("重新发送");
                $('.formgain').attr('id', 'mobileCodeBnt').attr('disabled', false);

                if ($('#orderNo').val() == '') {
                    $('#money').attr('readonly', true);
                }
                code = "";
            } else {

                curCount--;
                $(".formgain").empty().val("" + curCount + "秒重新发送");
            }
        }
    }
    /*--------------------------------充值------------------------------------------*/


    /*--------------------------------提现-----------------------------------------*/
    //查询可提现额度
    module.queryAccountInfo=function (data) {
        if(data.status==0){
            balance=data.data.balance;
            $('#balance').text(balance+'元')
        }
    };

    /*--------------------------------提现------------------------------------------*/


    module.init();
})();